<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20220315
 * Time: 11:05 
 * 角色权限管理
 */
namespace app\admin\controller;

use  Mscore\Core\Controller;
use  app\admin\model\Common as CommonModel;
use  app\admin\model\Web as WebModel;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lib\Upload;
use  Mscore\Core\Lang;


class WebController extends Controller{

	  private $model;
    private $login;
    private $error=['code'=>0,'msg'=>'fail'];
    private $success=['code'=>200,'msg'=>'success']; 

    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->model = new WebModel;
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    } 
    
    /**
     * 基础信息设置
     * @param array $basicsset 
     * @param array $where 
     */ 
    public function setconfig(){  
        $datainfo =$this->model->basicsset(1);
        $this->assign('datainfo',$datainfo);
        $this->display('basicsset.html');
    }

   /**
     * 基础信息保存
     * @param array $basicssave 
     * @param array $where 
     */ 
    public function basicssave(){
      $data = post();
      if($data['id']){
            $baimg = $this->model->getconfig('id='.$data['id'],'site_logo,site_logoback,site_favicon,site_logobottom,site_ewm,site_wxewm');
        }
        switch (true) {
            case isset($data['imgOne1'])&&!empty($data['imgOne1']):
                $url="/static/upload/site/";
                $site_logo = (new Upload)->Uploadbase64($data['imgOne1'],$url,"lo".time());
                if($baimg['site_logo']){
                   @unlink (config('file.path').$baimg['site_logo']);   
                }
                break;
            default:
                $site_logo=$baimg['site_logo'];
                break;
        }
        switch (true) {
            case isset($data['imgOne2'])&&!empty($data['imgOne2']):
                $url="/static/upload/site/";
                $site_logoback = (new Upload)->Uploadbase64($data['imgOne2'],$url,"lob".time());
                if($baimg['site_logoback']){
                   @unlink (config('file.path').$baimg['site_logoback']);   
                }
                break;
            default:
                $site_logoback=$baimg['site_logoback'];
                break;
        }
        switch (true) {
            case isset($data['imgOne3'])&&!empty($data['imgOne3']):
                $url="/static/upload/site/";
                $site_favicon = (new Upload)->Uploadbase64($data['imgOne3'],$url,"sfv".time());
                if($baimg['site_favicon']){
                   @unlink (config('file.path').$baimg['site_favicon']);   
                }
                break;
            default:
                $site_favicon=$baimg['site_favicon'];
                break;
        }
        switch (true) {
            case isset($data['imgOne4'])&&!empty($data['imgOne4']):
                $url="/static/upload/site/";
                $site_logobottom = (new Upload)->Uploadbase64($data['imgOne4'],$url,"lobt".time());
                if($baimg['site_logobottom']){
                   @unlink (config('file.path').$baimg['site_logobottom']);   
                }
                break;
            default:
                $site_logobottom=$baimg['site_logobottom'];
                break;
        }
        switch (true) {
            case isset($data['imgOne5'])&&!empty($data['imgOne5']):
                $url="/static/upload/site/";
                $site_ewm = (new Upload)->Uploadbase64($data['imgOne5'],$url,"ewm".time());
                if($baimg['site_ewm']){
                   @unlink (config('file.path').$baimg['site_ewm']);   
                }
                break;
            default:
                $site_ewm=$baimg['site_ewm'];
                break;
        }
        switch (true) {
            case isset($data['imgOne6'])&&!empty($data['imgOne6']):
                $url="/static/upload/site/";
                $site_wxewm = (new Upload)->Uploadbase64($data['imgOne6'],$url,"wx".time());
                if($baimg['site_wxewm']){
                   @unlink (config('file.path').$baimg['site_wxewm']);   
                }
            break;
            default:
                $site_wxewm=$baimg['site_wxewm'];
                break;
        }
        switch (true) {
            case isset($data['site_status'])&&!empty($data['site_status']):
                $site_status=1;
                break;
            default:
                $site_status=0;
                break;
        }
        $data=[
            'id'=>post('id'),
            'site_status'=>$site_status,
            'site_domain'=>$data['site_domain'],
            'site_name'=>$data['site_name'],
            'site_logo'=>$site_logo,
            'site_logoback'=>$site_logoback,
            'site_favicon'=>$site_favicon,
            'site_title'=>$data['site_title'],
            'site_keywords'=>$data['site_keywords'],
            'site_description'=>$data['site_description'],
            // 'site_statis'=>$data['site_statis'],
            'site_logobottom'=>$site_logobottom,
            'site_phone'=>$data['site_phone'],
            'site_email'=>$data['site_email'],
            'site_add'=>$data['site_add'],
            'site_ewm'=>$site_ewm,
            'site_wxewm'=>$site_wxewm,
            'site_copy'=>$data['site_copy'],
            'site_template'=>$data['site_template'],
            'site_custer'=>$data['site_custer']
        ];
        $info=$this->model->basicssave($data);
      switch ($info) {
        case true:
          $this->echoJson($this->success);
          break;        
        default:
          $this->echoJson($this->error);
          break;
      }  
    }

    /**
     * banner管理
     * @param array $setbanner 
     * @param array $where 
     */ 
  public function setbanner(){

       $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = '';
            $datalist = $this->model->advertlist($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->display('advert_list.html');
        }
  }

  /**
     * banner添加
     * @param array $add 
     * @param array $where 
     */ 
  public function add(){
    $params = post();
    if($params){
        //post请求则保存
        $info = $this->model->advertsave($params);
        switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }else{
        if(get('id')){
            $advert = $this->model->advertadd(get('id'));  
        }
        $advertclass = (new CommonModel)->advertclass();
        //定义变量identinfo输出
        $advert['id']=isset($advert['id'])?$advert['id']:'';
        $advert['title']=isset($advert['title'])?$advert['title']:'';
        $advert['titles']=isset($advert['titles'])?$advert['titles']:array();
        $advert['cid']=isset($advert['cid'])?$advert['cid']:'';
        $advert['introduction']=isset($advert['introduction'])?$advert['introduction']:array();
        $advert['imgurl']=isset($advert['imgurl'])?explode(',',$advert['imgurl']):[];
        $advert['chaintitle']=isset($advert['chaintitle'])?$advert['chaintitle']:array();
        $advert['chainurl']=isset($advert['chainurl'])?$advert['chainurl']:'';
        $advert['sortid']=isset($advert['sortid'])?$advert['sortid']:'';
        $advert['status']=isset($advert['status'])?$advert['status']:2;

        $langs=(new CommonModel)->getLang();
        $this->assign('langs',$langs);
        $this->assign('advertclass', $advertclass);
        $this->assign('advert', $advert);
        $this->display('advert_add.html');
    }       
  }
    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */ 
    public function del(){ 
         $info=$this->model->del(post('id',false));
         switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }
    
    /**
     * 类别添加
     * @param array $addcate 
     * @param array $where 
     */ 
    public function addcate(){
    //类别
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = '';
            $datalist = $this->model->advertclasslist($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            if(get('id')){
                $advert = $this->model->advertclassadd(get('id'));
            }
            $advert['id']=isset($advert['id'])?$advert['id']:'';
            $advert['title']=isset($advert['title'])?$advert['title']:'';
            $advert['advcode']=isset($advert['advcode'])?$advert['advcode']:'';
            $advert['icon']=isset($advert['icon'])?$advert['icon']:2;
            $advert['status']=isset($advert['status'])?$advert['status']:2;
            $this->assign('diInfo', $advert);
            $this->display('advertclass_add.html');
        }

    }
    
    /**
     * 类别保存
     * @param array $catesave 
     * @param array $where 
    */
    public function catesave(){
      $post = post();
      $info = $this->model->catesave($post);
      switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }

    /**
     * 广告类别删除
     * @param array $catedel 
     * @param array $where 
     */ 
    public function catedel(){ 
        $info=$this->model->catedel(post('id'));
        switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }

    /**
     * 菜单列表
     * @param array $menu 
     * @param array $where 
     */ 
    public function muelist(){
        $this->display('menu_list.html');
    }

    /**
     * 菜单列表数据
     * @param array $mue 
     * @param array $where 
     */ 
    public function mue(){
        $datalist = $this->model->mue();
            $data = array(
                "code" => 200,
                "data" => array_values($datalist),
                "count"=> 0,
                "msg" => 'true'
            );
            $this->echoJson($data);
    }
    /**
     * 菜单添加
     * @param array $mueadd 
     * @param array $where 
     */ 
    public function mueadd(){
        $post = post();
        switch (true) {
          case !empty($post):
            $info = $this->model->muesave($post);
            switch ($info) {
              case true:
                $this->echoJson($this->success);
                break;        
              default:
                $this->echoJson($this->error);
                break;
            }
            break;
          default:
            $pmenu=(new CommonModel())->getmenu('pid=0','id, title');
            if(get('id')){
                $diInfo = $this->model->mueadd(get('id'));
            }
            $diInfo['id']=isset($diInfo['id'])?$diInfo['id']:'';
            $diInfo['title']=isset($diInfo['title'])?$diInfo['title']:'';
            $datainfo['titles']=isset($datainfo['titles'])?$datainfo['titles']:array();
            $diInfo['url']=isset($diInfo['url'])?$diInfo['url']:'';
            $diInfo['pid']=isset($diInfo['pid'])?$diInfo['pid']:0;
            $diInfo['is_icon']=isset($diInfo['is_icon'])?$diInfo['is_icon']:0;
            $diInfo['icon']=isset($diInfo['icon'])?$diInfo['icon']:'';
            $diInfo['orderid']=isset($diInfo['orderid'])?$diInfo['orderid']:0;
            $diInfo['is_target']=isset($diInfo['is_target'])?$diInfo['is_target']:0;
            $diInfo['special']=isset($diInfo['special'])?$diInfo['special']:0;
            $diInfo['status']=isset($diInfo['status'])?$diInfo['status']:0;
            $diInfo['acode']=isset($diInfo['acode'])?$diInfo['acode']:'';
            $diInfo['pagecode']=isset($diInfo['pagecode'])?$diInfo['pagecode']:'';
            $diInfo['param']=isset($diInfo['param'])?$diInfo['param']:'';

            $langs=(new CommonModel)->getLang();
            $this->assign('langs',$langs);
            $this->assign('pmenu', $pmenu);
            $this->assign('diInfo', $diInfo);
            $this->display('menu_add.html');
            break;
        } 
    }

    /**
     * 菜单删除
     * @param array $muedel 
     * @param array $where 
     */ 
    public function muedel(){
       $info = $this->model->muedel(post('id'));
       switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        }
    }

    /**
     * 单项列表
     * @param array $single 
     * @param array $where 
     */ 
   public function single(){
        $post = post();
        if ($post) {
            $page = post('page',1);
            $limit = post('limit',10);
            $where = '';
            $datalist = $this->model->single($where,$page,$limit);
            $data = array(
                "code" => 200,
                "data" => $datalist['datalist'],
                "count"=> $datalist['total'],
                "msg" => 'ok'
            );
            $this->echoJson($data);
        } else {
            $this->display('single_list.html');
        }
    }
    /**
     * 單頁添加
     * @param array $singleadd 
     * @param array $where 
     */ 
    public function singleadd(){
        $post = post();
        switch (true) {
          case !empty($post):
            $info = $this->model->singlesave($post);
            switch ($info) {
              case true:
                $this->echoJson($this->success);
                break;        
              default:
                $this->echoJson($this->error);
                break;
            }
            break;
          default:
             if(get('id')){
                $diInfo = $this->model->singleadd(get('id'));
            }
            $sidinfo = $this->model->singlelist('type=1 and sid=0');
            $diInfo['id']=isset($diInfo['id'])?$diInfo['id']:'';
            $diInfo['title']=isset($diInfo['title'])?$diInfo['title']:'';
            $diInfo['titles']=isset($diInfo['titles'])?$diInfo['titles']:array();
            $diInfo['introduction']=isset($diInfo['introduction'])?$diInfo['introduction']:array();
            $diInfo['acode']=isset($diInfo['acode'])?$diInfo['acode']:'';
            $diInfo['orderid']=isset($diInfo['orderid'])?$diInfo['orderid']:0;
            $diInfo['status']=isset($diInfo['status'])?$diInfo['status']:0;
            $diInfo['url']=isset($diInfo['url'])?$diInfo['url']:'';
            $diInfo['imgurl']=isset($diInfo['imgurl'])?$diInfo['imgurl']:'';
            $diInfo['imgurls']=!empty($diInfo['imgurl'])?explode(",", $diInfo['imgurl']):[];
            $diInfo['videourl']=!empty($diInfo['videourl'])?$diInfo['videourl']:'';
            $diInfo['is_video']=!empty($diInfo['is_video'])?$diInfo['is_video']:1;
            $diInfo['orderid']=isset($diInfo['orderid'])?$diInfo['orderid']:0;
            $diInfo['type']=isset($diInfo['type'])?$diInfo['type']:0;
            $diInfo['cssid']=isset($diInfo['cssid'])?$diInfo['cssid']:0;
            $diInfo['is_index']=!empty($diInfo['is_index'])?$diInfo['is_index']:1;
            $diInfo['is_target']=isset($diInfo['is_target'])?$diInfo['is_target']:0;
            $diInfo['sid']=isset($diInfo['sid'])?$diInfo['sid']:0;

            $langs=(new CommonModel)->getLang();
            $this->assign('langs',$langs);
            $this->assign('sidinfo', $sidinfo);
            $this->assign('diInfo', $diInfo);
            $this->display('single_add.html');
            break;
        } 
    }

    /**
     * 单项删除
     * @param array $singledel 
     * @param array $where 
     */
   public function singledel(){
      $info =$this->model->singledel(post('id'));
      switch ($info) {
          case true:
            $this->echoJson($this->success);
            break;        
          default:
            $this->echoJson($this->error);
            break;
        } 
    }
    
}
